package bishi.day24_5_21;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * 迷宫问题:回溯
 * @author 是阿秋啊
 * @date 2022/05/22 14:26
 **/
public class mi_gong {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[][] arr = new int[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j< m; j++) {
                    arr[i][j] = sc.nextInt();
                }
            }
            List<List<Integer>> ret = new LinkedList<>();
            boolean flag = dfs(ret,arr,0,0);
            for (int i = 0; i < ret.size(); i++) {
                System.out.printf("(%d,%d)\n",ret.get(i).get(0),ret.get(i).get(1));
            }
        }
    }

    private static boolean dfs(List<List<Integer>> ret, int[][] arr, int i, int j) {
        if (i == arr.length || j == arr[0].length || i < 0 || j < 0 || arr[i][j] == 1) {
            return false;
        }
        if (i == arr.length - 1 && j == arr[0].length - 1) {
            List<Integer> tmp = new LinkedList<>();
            tmp.add(i);
            tmp.add(j);
            ret.add(tmp);
            return true;
        }else {
            List<Integer> tmp = new LinkedList<>();
            tmp.add(i);
            tmp.add(j);
            ret.add(tmp);
            arr[i][j] = 1;
            boolean flag = dfs(ret,arr,i+1,j) || dfs(ret,arr,i-1,j) || dfs(ret,arr,i,j+1) || dfs(ret,arr,i,j-1);
            if (!flag) {
                arr[i][j] = 0;
                ret.remove(ret.size()-1);
                return false;
            }else {
                return true;
            }
        }
    }
}